import myHttp from "@@/my-http";
import GuardUrl from "@@/guard-url";

export default function (){


    const urlList = GuardUrl.rbac.permission.lst
    const urlAdd = GuardUrl.rbac.permission.add
    let responseResult = ref<ResponseRecords>({
        records:[],
        total:0,
        msg:''
    })
    const loading = ref(false);
    let treePermissions = ref()


    function add(form:object) {
        loading.value = true
        myHttp.post(urlAdd,form).then(res=>{
            responseResult.value.msg = res.msg
        }).finally(()=>{
            loading.value = false
        })
    }


    async function getRecordsForTree() {
        await myHttp.get(urlList,{tree:1}).then(res=>{
            treePermissions.value = res.records
        }).finally(()=>{

        })
    }

    function getRecords() {
        loading.value = true
        myHttp.get(urlList).then(res=>{
            responseResult.value.records = res.records
            responseResult.value.total = res.total
            responseResult.value.msg = res.msg
        }).finally(()=>{
            loading.value = false
        })
    }

    return{
        responseResult,
        loading,
        treePermissions,
        getRecordsForTree,
        getRecords,
        add
    }


}
